

import { h, unref } from 'vue'
import { NRadio, NRadioGroup, NSpace } from 'naive-ui'
import { isFunction } from 'lodash'
import type { IJsonItem, IOption } from '../types'

export function renderRadio(item: IJsonItem, fields: { [field: string]: any }) {
  const { props, field, options } = isFunction(item) ? item() : item
  if (!options) {
    return h(NRadio, {
      ...props,
      value: fields[field],
      onUpdateChecked: (checked: boolean) => void (fields[field] = checked)
    })
  }
  return h(
    NRadioGroup,
    {
      ...props,
      value: fields[field],
      onUpdateValue: (value: any) => void (fields[field] = value)
    },
    () =>
      h(NSpace, null, () =>
        unref(options).map((option: IOption) =>
          h(NRadio, option, () => option.label)
        )
      )
  )
}
